package servlet;


import com.fasterxml.jackson.databind.ObjectMapper;
import domain.ResultInfo;
import domain.User;
import org.apache.commons.beanutils.BeanUtils;

import service.loginservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet(name="loginUserServlet",value = "/loginUserServlet")
public class loginUserServlet extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取用户名和密码数据
        User user = new User();
        String username = req.getParameter("username");

        String user_pwd = req.getParameter("password");


//3.调用 Service 查询
        loginservice service = new loginservice();
        User u = service.login(username,user_pwd);
        ResultInfo info = new ResultInfo();

//4.判断用户对象是否为 null
        if(u == null){
            //用户名密码或错误
            info.setFlag(false);
        }

       else{
            //登录成功
            info.setFlag(true);
            req.getSession().setAttribute("user",u);
        }
//响应数据

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(info.isFlag());

        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);
        System.out.println("1111");
        System.out.println(json);
        System.out.println("1111");



    }
}
